<?php

namespace app\h5\controller;


use app\admin\model\Tree;
use app\admin\model\TreeSituation;
use app\admin\model\TreeSituationRel;
use app\common\controller\Frontend;

class Policy extends Frontend
{
    protected array $noNeedLogin = ['index','policy'];


    public function index(): void
    {
//        $row_id = '502A22B3-074F-42EA-982F-BC4A16EFA929';
//        $row_id = '1E3104E3-64FC-48F3-9A19-0CEF6FB3B8B2';
        $param = $this->request->param();
        if(isset($param['type'])){
            if($param['type']=='zhuanti'){
                $row_id = '4E1C6462-4EAF-4B13-BA7B-521065C12E4B';
            }elseif($param['type']=='chanye'){
                $row_id = '860E05C3-FAEA-42B3-8F09-EA5D1EFC7BBD';
            }else{
                $row_id = '4E1C6462-4EAF-4B13-BA7B-521065C12E4B';
            }
        }else{
            $row_id = '4E1C6462-4EAF-4B13-BA7B-521065C12E4B';
        }
        if(isset($param['row_id'])){
            $pts_parent_node = $param['row_id'];
            $where[] = ['pts_parent_node','in',$pts_parent_node];
            $tree_list = TreeSituation::where([['pts_rowguid','in',$pts_parent_node]])->find();

        }else{
            $pts_parent_node = null;
            $where[] = ['pts_parent_node','=',$pts_parent_node];
        }

        $where[] = ['pt_rowguid','=',$row_id];
        $where[] = ['del_flag','=',0];


        $list = TreeSituation::with('answer')->where($where)->order('node_sort desc')->select();

        $arr = [];
        foreach($list as $key=>$val){
            if(!$val['answer']->isEmpty()){
                $arr_list = [];
                foreach ($val['answer'] as $v){
                    $arr_list[] = [
                        'value' => $v['pts_rowguid'],
                        'name' => $v['pts_name'],
                        'num' =>  TreeSituationRel::where('pts_rowguid',$v['pts_rowguid'])->count()
                    ];
                }
                if($val['pts_node_type']=='check'){
                    $type = 2;
                }elseif($val['pts_node_type']=='radio'){
                    $type = 1;
                }else{
                    $type = 'case';
                }
                $arr[] = [
                    'id' => $val['pts_rowguid'],
                    'type' => $type,
                    'pid' => $tree_list['pts_parent_node']??0,
                    'title' => $val['pts_name'],
                    'list' => $arr_list,
                    'current' => '',
                    'sum' => 0,
                    'number' => ($key+1).'.'
                ];
            }

        }
        $this->success('获取成功',$arr);

    }
    //获取政策
    public function policy(){
        $param = $this->request->param();
        if(!isset($param['pts_rowguid'])){
            $this->error('请传入节点ID');
        }
        $pts_rowguid = $param['pts_rowguid'];
        $where[] = ['pts_rowguid','in',$pts_rowguid];
        $list = TreeSituationRel::with(['policy'])->where($where)->select();
        $this->success('获取成功',$list);
    }
}